//
//  HYPhotoSelectViewCell.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/8.
//

#import "HYPhotoSelectViewCell.h"
#import <HYUIKit/UIView+Animation.h>

@interface HYPhotoSelectViewCell ()

@property (nonatomic, strong) UIImageView *selectImageView;

@property (nonatomic, strong) UIButton *selectPhotoButton;

@end

@implementation HYPhotoSelectViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        [self.contentView addSubview:self.selectImageView];
        [self.contentView addSubview:self.selectPhotoButton];
        [self.selectImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(3);
            make.right.mas_equalTo(-3);
            make.width.height.mas_equalTo(24);
        }];
        [self.selectPhotoButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.equalTo(self.contentView);
            make.width.height.mas_equalTo(44);
        }];
    }
    return self;
}

- (void)bindViewModel:(HYPhotoVideoItemViewModel *)vm
{
    [super bindViewModel:vm];
    self.selectPhotoButton.selected = vm.isSelect;
    [self.selectImageView setImage:[UIImage imageNamed:vm.isSelect?@"icon_select":@"icon_unSelect"]];
}

- (void)selectPhotoButtonClick:(UIButton *)selectPhotoButton
{
    BOOL maxSelect = self.itemVm.maxBlock();
    if(maxSelect && !selectPhotoButton.isSelected){
        [HYHub showMessage:NSLocalizedString(@"超出最大选择个数", nil)];
    }else{
        selectPhotoButton.selected = !selectPhotoButton.isSelected;
        self.itemVm.isSelect = selectPhotoButton.isSelected;
        [self.selectImageView setImage:[UIImage imageNamed:selectPhotoButton.isSelected?@"icon_select":@"icon_unSelect"]];
        [UIView showOscillatoryAnimationWithLayer:self.selectImageView.layer oscillatoryType:HYOscillatoryAnimation_Bigger];
        [self.itemVm.selectSubject sendNext:nil];
    }
}

#pragma mark - loadLazy

- (UIImageView *)selectImageView
{
    if (!_selectImageView) {
        _selectImageView = [[UIImageView alloc] init];
        _selectImageView.contentMode = UIViewContentModeCenter;
    }
    return _selectImageView;
}

- (UIButton *)selectPhotoButton
{
    if (!_selectPhotoButton) {
        _selectPhotoButton = [[UIButton alloc] init];
        [_selectPhotoButton addTarget:self action:@selector(selectPhotoButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectPhotoButton;
}

@end
